package com.H1023.controller;


import com.H1023.pojo.Book;
import com.H1023.pojo.Lend;
import com.H1023.pojo.Reader;
import com.H1023.pojo.Remand;
import com.H1023.service.LendService;
import com.H1023.service.ReaderService;
import com.mysql.cj.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Lend")
public class LendController { //借书控制层
    @Autowired
    @Qualifier("lendServiceImpl")
    private LendService lendService; //service层对象,调用service方法


    //在用户界面显示相应的图书信息
    @RequestMapping("/allBook")
    public String BookList(Model model,int bookid,int readerid){
        Book book = lendService.selectBookById(bookid);
        model.addAttribute("book",book);
        model.addAttribute("readerid",readerid);


        return "allBook";
    }

    //在管理员界面显示所有图书信息
    @RequestMapping("/adminAllBook")
    public String adminBookList(Model model){
        List<Book> bookList=lendService.selectAllBook();//查询所有图书
        model.addAttribute("list",bookList);//传送到前端显示
        return "adminAllBook";
    }

    //对管理员显示该书籍对应的借阅记录
    @RequestMapping("/adminQuery")
    public String adminQueryBookLendNote(Model model,int bookid){
        List<Lend> bookLendList = lendService.selectLendNoteByBookId(bookid);
        model.addAttribute("list",bookLendList);

        return "adminQueryBookNote";

    }

    //管理员通过搜索框模糊查询书籍名并显示对应的书籍列表，没有则为空
    @RequestMapping("/adminQueryBookByName")
    public String adminQueryByName(String queryBookName,Model model){
        List<Book> list=lendService.queryBookByName(queryBookName);
        model.addAttribute("list",list);
        return "adminAllBook";
    }
    //删除对应记录,在书籍对应的借阅记录页面删除
    @RequestMapping("/deleteLendNote")
    public String adminDeleteLendNote(Model model,int readerid,int bookid,String lenddate,String remanddate){
        Lend lend=new Lend();
        lend.setReaderid(readerid);
        lend.setLenddate(lenddate);
        lend.setRemanddate(remanddate);
        lendService.deleteLendNote(lend);
        List<Lend> bookLendList = lendService.selectLendNoteByBookId(bookid);
        model.addAttribute("list",bookLendList);
        return "adminQueryBookNote";
    }

    //显示所有借阅记录
    @RequestMapping("/adminAllLendNote")
    public String adminAllLendNote(Model model){
        List<Lend> lendList = lendService.selectAllLender();
        model.addAttribute("list",lendList);
        return "/adminAllLendNote";
    }

    //删除借书记录,在所有借书记录页面删除
    @RequestMapping("/deleteLendNote2")
    public String adminDeleteLendNote2(Model model,int readerid,int bookid,String lenddate,String remanddate){
        Lend lend=new Lend();
        lend.setReaderid(readerid);
        lend.setBookid(bookid);
        lend.setLenddate(lenddate);
        lend.setRemanddate(remanddate);
        lendService.deleteLendNote2(lend);
        List<Lend> lendList = lendService.selectAllLender();
        model.addAttribute("list",lendList);
        return "/adminAllLendNote";
    }



    //借书业务处理流程
    @RequestMapping("/readerLend")
    public String ReaderLend(int bookid,int readerId){

        Book book = lendService.selectBookById(bookid);
        if(book.getBookCount() == 0){
            return "redirect:/Lend/allBook";
        }
            Lend lend=new Lend();
            lend.setBookid(bookid);//填写书的ID
            lend.setReaderid(readerId);//填写读者的ID
            lend.setState(1);//填写书籍的归还状态
            lend.setLenddate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));//获取实时时间
            List<Lend> lendList= lendService.selectRepetitive(lend);//把所有读者id和书籍id相同的信息查出来放到list里
            for (Lend lendObject : lendList) {
                if(lendObject.getState() == 1){//查询发现用户已经借过此种书籍且没有还,借阅失败
                    return "redirect:/Lend/allBook";//重定向
                }
            }
            lendService.insertLendNote(lend);//借阅成功了,插入借阅信息
            lendService.updateBookCount(bookid);//书籍数量减一

        return "redirect:/Lend/allBook";
    }

    //读者在搜索框搜索图书，模糊查询
    @RequestMapping("/queryBookByName")
    public String queryBookByName(String queryBookName,Model model){
        List<Book> list=lendService.queryBookByName(queryBookName);
        model.addAttribute("list",list);
        return "allBook";
    }


    //显示读者借书记录
    @RequestMapping("/record")
    public String record(Model model,int readerid){

        List<Lend> recordList=lendService.selectAllLenderById(readerid);//查询
        model.addAttribute("recordList",recordList);//传送到前端显示


        return "record";
    }




}
